<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>

	<head>
		<title>业务处理-领料归还-新增领料归还</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
	</head>

	<body>
		<div id="mainContent">
			<div id="mainContent_content">
				<div class="page_header">
					<a href="<%=basePath%>pickingReturnContrlooer/selectByWhere.do">领料归还</a>
					<i class="icon-double-right"></i>
					<a>新增领料归还</a>
				</div>

				<div class="btn rightBtn">
					<input type="button" value="选择领料出库订单" onclick="toAddPurchaseRequestItem();" class="btn btn2"></input>
					<input type="button" value="保存" onclick="saveAddPurchaseRequest();" class="btn btn1"></input>
					<input type="button" value="返回" onclick="location.href='<%=basePath%>pickingReturnContrlooer/selectByWhere.do'" class="btn btn2"></input>
				</div>
				<form id="generalStorageForm" class="form">
					<div id="requisitionForm" class="box">
						<p>
							<label> 入库单号</label>
							<input type="text" readonly placeholder="自动生成" class="form-control"></input>

							<label style="width: 80px;"> 领料归还人</label>
							<input type="text" readonly value="${userName }" class="form-control"></input>
							<input type="hidden" value="${userID }" name="buyerID"></input>
							<input type="hidden" value="${userID }" name="createPersonID"></input>
							<input type="hidden" value="${userID }" name="lastPersonID"></input>

							<label> 状态</label>
							<select name="status" class="form-control select">
								<option value="0">未提交</option>
								<option value="1">已提交</option>
							</select>

						</p>
						<p>
							<label> 仓库</label>
							<select name="warehouseID" onchange="gradeChange()" class="form-control select">
								<c:forEach items="${warehouseList }" var="warehouse">
									<option value="${warehouse.warehousemanagementid }">${warehouse.warehousename }</option>
								</c:forEach>
							</select>
							<label> 说明</label>
							<input type="text" name="company" class="form-control"></input>

							<label>备注</label>
							<input type="text" name="remarks" class="form-control doubleWidth"></input>

							<input type="hidden" name="pickingID">
						</p>
					</div>
					<div class="tableBox box">
						<table class="table table-bordered table_text_center">
							<thead>
								<tr>
									<th>领料出库单号</th>
									<th>代码</th>
									<th>物品名称</th>
									<th>物品条码</th>
									<th>品牌</th>
									<th>库存</th>
									<th>总归还数量</th>
									<th>归还数量</th>
									<th>规格</th>
									<th>归还小计</th>
									<th>领用出库数量</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="purchaseRequestItemTbody">
							</tbody>
						</table>

					</div>
				</form>
			</div>
			<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
			<jsp:include page="/jsp/storage/pickingReturn/addPurchaseRequestItem.jsp"></jsp:include>
		</div>
		<script src="<%=basePath%>js/jquery-3.2.1.min.js"></script>
		<script src="<%=basePath%>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
		<script src="<%=basePath%>My97DatePickers/WdatePicker.js"></script>
		<script src="<%=basePath%>js/public.js"></script>
		<script type="text/javascript">
			function gradeChange() {
				$('#purchaseRequestItemTbody').empty();
			}

			$(function() {
				// 任何需要执行的js特效 
			});

			function addToHasAddTable(item) {
				$.ajax({
					url: "<%=basePath%>pickingReturnContrlooer/listAddItem2.do",
					type: "post",
					async: false,
					dataType: "json",
					data: "pickingID=" + item.pickingID,
					success: function(data) {
						$("#generalStorageForm [name='pickingID']").val(item.pickingID);
						var html = "";
						$.each(data, function(i, n) {

							html += "<tr>";
							html += "<td>" + data[i].picking1.pickingNumber + "</td>";
							html += "<td>" + data[i].PickingItem.itemCode + "</td>";
							html += "<td>" + data[i].PickingItem.chineseName + "</td>";
							html += "<td>" + data[i].PickingItem.articleBarCode + "</td>";
							html += "<td>" + data[i].PickingItem.brand + "</td>";
							html += "<td>" + data[i].PickingItem.currentStock + "</td>";
							html += "<td><input class='form-control' style='width: 60px;' name='pickingreturnNumber' type='text' readonly value='" + data[i].PickingItem.pickingreturnNumber + "'/></td>";
							html += "<td><input class='form-control' style='width: 60px;' name='itemNumber2' maxlength='8'  oninput='calculations(this)' type='text' value='" + (data[i].PickingItem.number - data[i].PickingItem.pickingreturnNumber) + "'/></td>";
							html += "<td><select class='form-control select' oninput='calculations2(this)'>" +
								"<option value='" + data[i].PickingItem.salesSpecifications + "'>" + data[i].PickingItem.salesSpecifications + "" + data[i].PickingItem.salesUnit + "</option>" +
								"<option value='" + data[i].PickingItem.purchaseSpecifications + "'>" + data[i].PickingItem.purchaseSpecifications + "" + data[i].PickingItem.salesUnit + "/" + data[i].PickingItem.purchasingUnit + "</option>" +
								"</select></td>";
							html += "<td><input class='form-control' style='width: 60px;' type='text' name='itemNumber' readonly value='" + (data[i].PickingItem.number - data[i].PickingItem.pickingreturnNumber) + "'/></td>";
							html += "<td><input class='form-control' style='width: 60px;' name='pickingItemNumber' type='text' readonly value='" + data[i].PickingItem.number + "'/></td>";
							/* html+="<td><input type='text' name='itemNumber' onkeyup='limitInputNumber(this);' onchange='limitInputNumber(this);'/></td>"; */
							html += "<td><a class='btn btn-xs btn_danger' onclick='deleteItem(this);'>删除</a>";
							html += "<input type='hidden' name='item_detailId' value='" + data[i].PickingItem.sourceID + "' />";
							html += "<input type='hidden' name='id' value='" + data[i].PickingItem.id + "' />";
							html += "<input type='hidden' name='itemIds' value='" + data[i].PickingItem.intemInformationID + "' /></td>";
							html += "</tr>";
						});

						$("#purchaseRequestItemTbody").append(html);
					},
					error: function() {
						Prompt.alert("提示", "根据订单查询物品失败！");
					}
				});
			}
			/* 输入采购数量自动计算数量 */
			function calculations(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).val().replace(/\D/g, '')) * ($(obj).parent().next().find("option:selected").val()));
			}
			/* 选择规格下拉框在自动计算数量 */
			function calculations2(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).parent().parent().find("input[name='itemNumber2']").val().replace(/\D/g, '')) * ($(obj).find("option:selected").val()));
			}

			function deleteItem(obj) {
				$(obj).parent().parent().remove();
			}

			function validateForm() {
				/* 检查是否新增了物品 */
				var length = $("#purchaseRequestItemTbody [name='itemIds']").length;
				if(!length > 0) {
					Prompt.alert("提示", "物品种类数量必须大于0！")
					return false;
				}
				/* 检查数量是否大于0 */
				var arr = $("#purchaseRequestItemTbody [name='itemNumber']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '' || num <= 0) {
						Prompt.alert("提示", "物品归还数量必须大于0！")
						return false;
					}
				}
				/* 检查归还小计数量是否大于领用出库数量 */
				var arr = $("#purchaseRequestItemTbody [name='itemNumber']");
				var pickingreturnNumberArr = $("#purchaseRequestItemTbody [name='pickingreturnNumber']");
				var pickingItemNumberArr = $("#purchaseRequestItemTbody [name='pickingItemNumber']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					var num1 = $(pickingreturnNumberArr[i]).val();
					var num2 = $(pickingItemNumberArr[i]).val();

					if(num.trim() == '' || num <= 0 ||
						num2.trim() == '' || num2 <= 0 ||
						(num2 - num1 < num)) {
						Prompt.alert("提示", "物品归还数量必须小于(领用出库数量（减去）已归还数量的（差）)！")
						return false;
					}
				}
				return true;
			}

			function saveAddPurchaseRequest() {
				if(!validateForm()) return;
				$.ajax({
					url: "<%=basePath%>pickingReturnContrlooer/add.do",
					type: "post",
					async: false,
					dataType: "json",
					data: $("#generalStorageForm").serialize(),
					success: function(data) {
						if(data.result == "success") {
							Prompt.alert("提示", "新增领料归还保存成功！", function() {
								location.href = "<%=basePath%>pickingReturnContrlooer/selectByWhere.do";
							});
						} else {
							Prompt.alert("提示", "新增领料归保存失败！");
						}
					},
					error: function() {
						Prompt.alert("提示", "新增领料归保存失败！");
					}
				});
			}
		</script>
	</body>

</html>